﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProjectCollectionHandler.cs" company="Scrum for Team System">
//   None
// </copyright>
// <summary>
//   The project collection handler.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ScrumForTeamSystem.Setup.SfTSDeployment
{
    using System;
    using System.Collections.Generic;

    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Framework.Client;
    using Microsoft.TeamFoundation.Framework.Common;

    /// <summary>
    /// The project collection handler.
    /// </summary>
    public class ProjectCollectionHandler
    {
        /// <summary>
        /// The team foundation application instance.
        /// </summary>
        private readonly TfsConfigurationServer tfsConfigurationServer;

        /// <summary>
        /// The team project collection service.
        /// </summary>
        private readonly ITeamProjectCollectionService teamProjectCollectionService;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectCollectionHandler"/> class.
        /// </summary>
        /// <param name="tfsurl">
        /// The tfsurl.
        /// </param>
        public ProjectCollectionHandler(string tfsurl)
        {
            this.tfsConfigurationServer = TfsHelper.GetTfsInstance(tfsurl);

            if (this.tfsConfigurationServer == null)
            {
                throw new Exception("Unable to load TFS applciation instance.");
            }

            this.teamProjectCollectionService =
                this.tfsConfigurationServer.GetService<ITeamProjectCollectionService>();

            this.TfsUri = this.tfsConfigurationServer.Uri;
        }

        /// <summary>
        /// Gets the TFS URI.
        /// </summary>
        /// <value>The TFS URI.</value>
        public Uri TfsUri { get; private set; }

        /// <summary>
        /// The list collections.
        /// </summary>
        /// <returns>
        /// return a list 
        /// </returns>
        public List<ProjectCollectionInfo> ListCollections()
        {
            var projectCollections = new List<ProjectCollectionInfo>();
            var teamProjectCollections = (List<TeamProjectCollection>)this.teamProjectCollectionService.GetCollections();

            teamProjectCollections.ForEach(
                tpc =>
                projectCollections.Add(
                    new ProjectCollectionInfo(
                    this.tfsConfigurationServer, 
                    tpc.Id, 
                    tpc.Name, 
                    tpc.State == TeamFoundationServiceHostStatus.Started ? true : false, 
                    tpc.VirtualDirectory.Replace("~", this.tfsConfigurationServer.Uri.AbsoluteUri))));

            return projectCollections;
        }
    }
}